package com.neusoft.crm.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neusoft.crm.common.R;
import com.neusoft.crm.mapper.DataAreaMapper;
import com.neusoft.crm.mapper.SysUserMapper;
import com.neusoft.crm.po.DataArea;
import lombok.extern.log4j.Log4j2;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.crm.po.SysUser;
import com.neusoft.crm.service.ISysUserService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@Log4j2
public class SysUserController {

	@Resource
	private ISysUserService sysUserService;

	@Resource
	private SysUserMapper sysUserMapper;

	@Resource
	private DataAreaMapper dataAreaMappper;
	
	@RequestMapping("/selectSysUserByNameByPass")
	@Transactional
	public R<SysUser> selectSysUserByNameByPass(HttpServletRequest request,@RequestBody SysUser sysUser) {
		String password =sysUser.getUserPassword();
		LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(SysUser::getUserName, sysUser.getUserName());
		SysUser emp =sysUserService.getOne(queryWrapper);
		//如果没有查询结果则登陆失败结果
		if (emp == null ) {
			return R.error("用户名或密码错误");
		}
		//密码对比，如果不一致则返回登陆结果
		if (!emp.getUserPassword().equals(password)){
			return R.error("用户名或密码错误");
		}
		request.getSession().setAttribute("userName", emp.getUserName());
		return R.success(emp);
	}

	@RequestMapping("/selectAllkhjl")
	public List<SysUser> selectAllkhjl(@RequestBody SysUser sysUser) {
		QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
		wrapper.eq("user_Role_Id",3);
		return sysUserMapper.selectList(wrapper);
	}

	@RequestMapping("/selectAllSysUserInfo")
	public List<SysUser> selectAllSysUserInfo(){
		return sysUserMapper.selectAllSysUserInfo();
	}

	@RequestMapping("/deleteSysUserInfoData")
	public int deleteSysUserInfoData(@RequestBody SysUser sysUser){
		return sysUserMapper.deleteById(sysUser);
	}

	@RequestMapping("/addSysUserInfoData")
	public int addSysUserInfoData(@RequestBody SysUser sysUser){
		return sysUserMapper.insert(sysUser);
	}


}
